package com.xyz.keshe;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class AdminHomeActivity extends AppCompatActivity {

    private TextView tvWelcome;
    private Button btnNewsManage;
    private Button btnCategoryManage;
    private Button btnAnnouncementManage;
    private Button btnAdminManage;
    private Button btnLogout;
    
    private int adminId;
    private String adminUsername;
    private boolean isSuper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_home);

        // 获取传递的管理员信息
        Intent intent = getIntent();
        adminId = intent.getIntExtra("admin_id", 0);
        adminUsername = intent.getStringExtra("admin_username");
        isSuper = intent.getBooleanExtra("admin_is_super", false);

        // 初始化视图
        tvWelcome = findViewById(R.id.tv_welcome);
        btnNewsManage = findViewById(R.id.btn_news_manage);
        btnCategoryManage = findViewById(R.id.btn_category_manage);
        btnAnnouncementManage = findViewById(R.id.btn_announcement_manage);
        btnAdminManage = findViewById(R.id.btn_admin_manage);
        btnLogout = findViewById(R.id.btn_logout);

        // 设置欢迎文本
        tvWelcome.setText("欢迎，" + adminUsername + (isSuper ? " (超级管理员)" : ""));

        // 只有超级管理员可以管理其他管理员
        btnAdminManage.setVisibility(isSuper ? View.VISIBLE : View.GONE);

        // 设置按钮点击事件
        btnNewsManage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AdminHomeActivity.this, NewsManageActivity.class);
                startActivity(intent);
            }
        });

        btnCategoryManage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AdminHomeActivity.this, CategoryManageActivity.class);
                startActivity(intent);
            }
        });

        btnAnnouncementManage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AdminHomeActivity.this, AnnouncementManageActivity.class);
                startActivity(intent);
            }
        });

        btnAdminManage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AdminHomeActivity.this, AdminManageActivity.class);
                startActivity(intent);
            }
        });

        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 返回欢迎页面
                Intent intent = new Intent(AdminHomeActivity.this, WelcomeActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                // Intent.FLAG_ACTIVITY_CLEAR_TOP
//                作用：如果目标 Activity（这里是 WelcomeActivity）已经在任务栈中存在，则清除它之上的所有 Activity，使其位于栈顶。
//                效果：不会创建新的 WelcomeActivity 实例，而是复用已有的实例，并销毁它上面的所有其他 Activity。
                //Intent.FLAG_ACTIVITY_NEW_TASK
                //作用：在新的任务栈中启动目标 Activity。
                //效果：通常用于从非 Activity 的上下文（如 Service 或 Broadcast）启动 Activity，但在这里与 FLAG_ACTIVITY_CLEAR_TOP 联合使用，确保 WelcomeActivity 成为任务栈的根 Activity。
                startActivity(intent);
                finish();
            }
        });
    }
} 